package com.diandian.backend.store.service;

import com.alibaba.nacos.shaded.com.google.common.collect.Lists;
import com.diandian.backend.store.domain.StoreOfflineStoreInfo;
import com.diandian.backend.store.dto.*;
import com.diandian.backend.store.mapper.StoreOfflineStoreInfoMapper;
import com.diandian.backend.store.resp.StoreOfflineStoreInfoDetailResp;
import com.diandian.backend.store.resp.StoreOfflineStoreInfoListResp;
import com.diandian.backend.store.resp.StoreOfflineStoreInfoPageResp;
import com.diandian.base.boot.starter.exception.DefaultException;
import com.diandian.base.boot.starter.response.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Service
public class StoreOfflineStoreInfoService {

    @Autowired
    private StoreOfflineStoreInfoMapper storeOfflineStoreInfoMapper;

    public PageResult page(StoreOfflineStoreInfoPageParamDTO requestDTO){

        requestDTO.check();
        int pageNum = requestDTO.getPageNum();
        int pageSize = requestDTO.getPageSize();

        // 查询总数
        int totalCount = storeOfflineStoreInfoMapper.getCount(requestDTO);

        // 查询数据集合
        List<StoreOfflineStoreInfo> dataList = storeOfflineStoreInfoMapper.getPage(requestDTO, requestDTO.getPageSize(), (requestDTO.getPageNum() - 1) * requestDTO.getPageSize());

        // 组装数据
        List<StoreOfflineStoreInfoPageResp> respList = new ArrayList<>();
        if(dataList != null && dataList.size() > 0){
            dataList.stream().forEach( x ->{
                StoreOfflineStoreInfoPageResp item = new StoreOfflineStoreInfoPageResp();
                item.setId(x.getId());
                item.setCode(x.getCode());
                item.setName(x.getName());
                item.setLogo(x.getLogo());
                item.setContactUserName(x.getContactUserName());
                item.setContactTel(x.getContactTel());
                item.setAddress(x.getAddress());
                item.setCoordinate(x.getCoordinate());
                item.setRemark(x.getRemark());
                item.setBelong(x.getBelong());
                item.setCreateUserId(x.getCreateUserId());
                item.setCreateUserName(x.getCreateUserName());
                item.setCreateTime(x.getCreateTime());
                item.setOrgId(x.getOrgId());
                item.setOrgCode(x.getOrgCode());
                item.setUpdateUserId(x.getUpdateUserId());
                item.setUpdateUserName(x.getUpdateUserName());
                item.setUpdateTime(x.getUpdateTime());
                item.setDel(x.getDel());
             respList.add(item);
            });
        }

        return new PageResult(pageNum, pageSize, totalCount, respList);
    }

    public List<StoreOfflineStoreInfoListResp> getList(StoreOfflineStoreInfoListParamDTO requestDTO){

        // 查询数据集合
        List<StoreOfflineStoreInfo> dataList = storeOfflineStoreInfoMapper.getList(requestDTO);

        // 组装数据
        List<StoreOfflineStoreInfoListResp> respList = new ArrayList<>();
        if(dataList != null && dataList.size() > 0){
            dataList.stream().forEach( x ->{
                StoreOfflineStoreInfoListResp item = new StoreOfflineStoreInfoListResp();
                item.setId(x.getId());
                item.setCode(x.getCode());
                item.setName(x.getName());
                item.setLogo(x.getLogo());
                item.setContactUserName(x.getContactUserName());
                item.setContactTel(x.getContactTel());
                item.setAddress(x.getAddress());
                item.setCoordinate(x.getCoordinate());
                item.setRemark(x.getRemark());
                item.setBelong(x.getBelong());
                item.setCreateUserId(x.getCreateUserId());
                item.setCreateUserName(x.getCreateUserName());
                item.setCreateTime(x.getCreateTime());
                item.setOrgId(x.getOrgId());
                item.setOrgCode(x.getOrgCode());
                item.setUpdateUserId(x.getUpdateUserId());
                item.setUpdateUserName(x.getUpdateUserName());
                item.setUpdateTime(x.getUpdateTime());
                item.setDel(x.getDel());
                respList.add(item);
            });
        }

        return respList;
    }

    public StoreOfflineStoreInfoDetailResp getDetail(StoreOfflineStoreInfoDetailParamDTO param) {

        StoreOfflineStoreInfo dbData = storeOfflineStoreInfoMapper.getDetail(param);
        if(dbData == null){
            return null;
        }

        StoreOfflineStoreInfoDetailResp result = new StoreOfflineStoreInfoDetailResp();
        result.setId(dbData.getId());
        result.setCode(dbData.getCode());
        result.setName(dbData.getName());
        result.setLogo(dbData.getLogo());
        result.setContactUserName(dbData.getContactUserName());
        result.setContactTel(dbData.getContactTel());
        result.setAddress(dbData.getAddress());
        result.setCoordinate(dbData.getCoordinate());
        result.setRemark(dbData.getRemark());
        result.setBelong(dbData.getBelong());
        result.setCreateUserId(dbData.getCreateUserId());
        result.setCreateUserName(dbData.getCreateUserName());
        result.setCreateTime(dbData.getCreateTime());
        result.setOrgId(dbData.getOrgId());
        result.setOrgCode(dbData.getOrgCode());
        result.setUpdateUserId(dbData.getUpdateUserId());
        result.setUpdateUserName(dbData.getUpdateUserName());
        result.setUpdateTime(dbData.getUpdateTime());
        result.setDel(dbData.getDel());
        return result;
    }

    public void updateById(StoreOfflineStoreInfoUpdateByIdParamDTO param) {
        param.check();

        StoreOfflineStoreInfoDetailParamDTO queryDetailDTO = new StoreOfflineStoreInfoDetailParamDTO();
        queryDetailDTO.setId(param.getId());
        StoreOfflineStoreInfo dbData = storeOfflineStoreInfoMapper.getDetail(queryDetailDTO);
        if(dbData == null){
            throw new DefaultException("更新的记录不存在");
        }

        storeOfflineStoreInfoMapper.updateById(param);
    }

    public void updateBasicInfo(StoreOfflineStoreInfoUpdateByIdParamDTO param) {
        param.check();
        storeOfflineStoreInfoMapper.updateBasicInfo(param);
    }

    public void deleteById(StoreOfflineStoreInfoDeleteByIdParamDTO paramDTO) {
        paramDTO.check();
        storeOfflineStoreInfoMapper.deleteById(paramDTO);
    }

    public void add(StoreOfflineStoreInfoAddParamDTO param) {
        param.setCreateTime(new Date());
        param.setDel(0);
        storeOfflineStoreInfoMapper.add(param);
    }

    public void batchAdd(List<StoreOfflineStoreInfoAddParamDTO> dataList) {
        if(dataList == null || dataList.size() == 0){
            return;
        }

        List<List<StoreOfflineStoreInfoAddParamDTO>> batchList = Lists.partition(dataList, 100);
        for (int i = 0; i < batchList.size(); i++) {
            List<StoreOfflineStoreInfoAddParamDTO> batchItemList = batchList.get(i);
            for (int j = 0; j < batchItemList.size(); j++) {
                StoreOfflineStoreInfoAddParamDTO dataItem = batchItemList.get(j);
                dataItem.setCreateTime(new Date());
                dataItem.setDel(0);
            }
            storeOfflineStoreInfoMapper.batchAdd(batchItemList);
        }
    }

    public void batchUpdate(List<StoreOfflineStoreInfoAddParamDTO> dataList) {
        if(dataList == null || dataList.size() == 0){
            return;
        }

        List<List<StoreOfflineStoreInfoAddParamDTO>> batchList = Lists.partition(dataList, 100);
        for (int i = 0; i < batchList.size(); i++) {
            List<StoreOfflineStoreInfoAddParamDTO> batchItemList = batchList.get(i);
            for (int j = 0; j < batchItemList.size(); j++) {
                StoreOfflineStoreInfoAddParamDTO dataItem = batchItemList.get(j);
                dataItem.setUpdateTime(new Date());
            }
            storeOfflineStoreInfoMapper.batchUpdate(batchItemList);
        }
    }
}
